package com.finance.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.finance.web.domain.FmAccount;
import com.finance.web.service.IFmAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.finance.common.annotation.Log;
import com.finance.common.enums.BusinessType;
import com.finance.web.domain.FmJournal;
import com.finance.web.service.IFmJournalService;
import com.finance.common.core.controller.BaseController;
import com.finance.common.core.domain.AjaxResult;
import com.finance.common.utils.poi.ExcelUtil;
import com.finance.common.core.page.TableDataInfo;

/**
 * 日记账Controller
 *
 * @author lisen
 * @date 2021-11-11
 */
@Controller
@RequestMapping("/admin/journal")
public class FmJournalController extends BaseController
{
    private String prefix = "admin/journal";

    @Autowired
    private IFmJournalService fmJournalService;
    @Autowired
    private IFmAccountService fmAccountService;

    @RequiresPermissions("admin:journal:view")
    @GetMapping()
    public String journal(ModelMap mmap)
    {
        List<FmAccount> list = fmAccountService.selectFmAccountList(new FmAccount());
        Map<Long,String> accountMap = new HashMap<>();
        for (FmAccount account : list) {
            accountMap.put(account.getId(), account.getName());
        }
        mmap.put("accountMap",accountMap);
        return prefix + "/journal";
    }

    /**
     * 查询日记账列表
     */
    @RequiresPermissions("admin:journal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FmJournal fmJournal)
    {
        startPage();
        List<FmJournal> list = fmJournalService.selectFmJournalList(fmJournal);
        return getDataTable(list);
    }

    /**
     * 导出日记账列表
     */
    @RequiresPermissions("admin:journal:export")
    @Log(title = "日记账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FmJournal fmJournal)
    {
        List<FmJournal> list = fmJournalService.selectFmJournalList(fmJournal);
        ExcelUtil<FmJournal> util = new ExcelUtil<FmJournal>(FmJournal.class);
        return util.exportExcel(list, "日记账数据");
    }

    /**
     * 新增日记账
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存日记账
     */
    @RequiresPermissions("admin:journal:add")
    @Log(title = "日记账", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FmJournal fmJournal)
    {
        fmJournal.setUserId(getSysUser().getUserId());
        fmJournal.setUserName(getSysUser().getUserName());
        return toAjax(fmJournalService.insertFmJournal(fmJournal));
    }

    /**
     * 修改日记账
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FmJournal fmJournal = fmJournalService.selectFmJournalById(id);
        mmap.put("fmJournal", fmJournal);
        return prefix + "/edit";
    }

    /**
     * 修改保存日记账
     */
    @RequiresPermissions("admin:journal:edit")
    @Log(title = "日记账", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FmJournal fmJournal)
    {
        return toAjax(fmJournalService.updateFmJournal(fmJournal));
    }

    /**
     * 删除日记账
     */
    @RequiresPermissions("admin:journal:remove")
    @Log(title = "日记账", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fmJournalService.deleteFmJournalByIds(ids));
    }
}
